package com.javaProject.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.javaProject.dto.R;
import com.javaProject.entity.Message;
import com.javaProject.entity.Student;
import com.javaProject.entity.Teacher;
import com.javaProject.mapper.StudentMapper;
import com.javaProject.mapper.TeacherMapper;
import com.javaProject.service.MessageService;
import com.javaProject.mapper.MessageMapper;
import com.javaProject.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
* @author ASUS
* @description 针对表【message】的数据库操作Service实现
* @createDate 2024-03-19 14:36:45
*/
@Service
public class MessageServiceImpl extends ServiceImpl<MessageMapper, Message>
    implements MessageService{

    @Autowired
    TeacherMapper teacherMapper;

    @Autowired
    StudentMapper studentMapper;

    @Override
    public R sendAllteacher(Message message) {
        if (message == null) {
            return R.fail().setMsg("消息为空");
        }
        List<Teacher> teachers =  teacherMapper.selectList(null);
        List<Message> messages =  new ArrayList<>();
        Date dateTime = new Date();
        System.out.println(teachers);
        for (Teacher teacher:teachers){
            Message message1 = new Message();
            message1.setRecipientId(teacher.getTNub());
            message1.setTitle(message.getTitle());
            message1.setContent(message.getContent());
            message1.setTimestamp(dateTime);
            messages.add(message1);
        }

        if (saveBatch(messages)){
            return  R.ok();
        }

        return R.fail().setMsg("消息发送失败");
    }

    @Override
    public R sendAllStudent(Message message) {
        List<Student> students = studentMapper.selectList(null);
        List<Message> messages =  new ArrayList<>();
        Date dateTime = new Date();
        for (Student student:students){
            Message message1 = new Message();
            message1.setRecipientId(student.getStudentId());
            message1.setTitle(message.getTitle());
            message1.setContent(message.getContent());
            message1.setTimestamp(dateTime);
            messages.add(message1);
        }
        if (saveBatch(messages)){
            return  R.ok();
        }

        return R.fail().setMsg("消息发送失败");
    }

    @Override
    public R sendMessage(Message message) {
        Date dateTime = new Date();
        message.setTimestamp(dateTime);
        if (save(message)){
            return R.ok();
        }
        return R.fail().setMsg("消息发送失败");

    }

    @Override
    public List<Message> getOneAllMessage(Message message) {
        QueryWrapper<Message> query = Wrappers.query(Message.class);
        query.eq("recipient_id",message.getRecipientId());
        List<Message> messages = list(query);
        return messages;

    }

    @Override
    public R isReadMessage(Message message) {
        QueryWrapper<Message> query =Wrappers.query(Message.class);
        query.eq("message_id",message.getMessageId());
        Message one = getOne(query);
        if (Objects.isNull(one)){
            return R.fail().setMsg("消息不存在");
        }
        one.setIsRead('Y');
        if (update(one,query)){
            return R.ok().setData(one);
        }
        return R.fail().setMsg("修改失败");
    }


}




